#include <string.h>
#include "lib.h"
#ifdef WIN32
#include "sys_call_pc.h"
#else 
#include "sys_call.h"
#endif

//-------------------------------------------------------------------------
//
//-------------------------------------------------------------------------
time_t array2timet(U8 *p, int nIsBcd)
{
	struct tm tmTime;

	if (nIsBcd) {
		tmTime.tm_year = bcd2bin8(*p++) + 100;
		tmTime.tm_mon = bcd2bin8(*p++) - 1;
		tmTime.tm_mday = bcd2bin8(*p++);
		tmTime.tm_hour = bcd2bin8(*p++);
		tmTime.tm_min = bcd2bin8(*p++);
		tmTime.tm_sec = bcd2bin8(*p++);
	} else {
		tmTime.tm_year = *p + 100;
		tmTime.tm_mon = *p++ - 1;
		tmTime.tm_mday = *p++;
		tmTime.tm_hour = *p++;
		tmTime.tm_min = *p++;
		tmTime.tm_sec = *p++;
	}
	return mktime(&tmTime);
}

//-------------------------------------------------------------------------
//
//-------------------------------------------------------------------------
int timet2array(time_t tTime, U8 *p, int nIsBcd)
{
	struct tm tmTime;

	localtime_r(&tTime, &tmTime);
	if (nIsBcd) {
		*p++ = bin2bcd8(tmTime.tm_year - 100);
		*p++ = bin2bcd8(tmTime.tm_mon + 1);
		*p++ = bin2bcd8(tmTime.tm_mday);
		*p++ = bin2bcd8(tmTime.tm_hour);
		*p++ = bin2bcd8(tmTime.tm_min);
		*p = bin2bcd8(tmTime.tm_sec);
	} else {
		*p++ = tmTime.tm_year - 100;
		*p++ = tmTime.tm_mon + 1;
		*p++ = tmTime.tm_mday;
		*p++ = tmTime.tm_hour;
		*p++ = tmTime.tm_min;
		*p = tmTime.tm_sec;
	}
	return 1;
}


